package com.returnsoft.security.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.security.dto.DepartmentDto;
import com.returnsoft.security.eao.DepartmentEao;
import com.returnsoft.security.entity.Department;
import com.returnsoft.security.exception.ConversionExcepcion;
import com.returnsoft.security.exception.EaoExcepcion;
import com.returnsoft.security.exception.SecurityExcepcion;
import com.returnsoft.security.util.Conversion;



/**
 * Session Bean implementation class DepartmentBean
 */
@Stateless
@LocalBean
public class DepartmentBean implements DepartmentInterface ,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private DepartmentEao eao;
	@EJB
	private Conversion conv;

	@Override
	public List<DepartmentDto> findDepartmentsParent() throws SecurityExcepcion {
		try {
			List<Department> departments = eao.findDepartmentsParent();
			List<DepartmentDto> departmentsDto = new ArrayList<DepartmentDto>();
			for (Department department : departments) {
				DepartmentDto departmentDto = conv.fromDepartment(department);
				departmentsDto.add(departmentDto);
			}
			return departmentsDto;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}

	@Override
	public List<DepartmentDto> findDepartmentsChild(Integer departmentId)
			throws SecurityExcepcion {
		try {
			List<Department> departments = eao
					.findDepartmentsChild(departmentId);
			List<DepartmentDto> departmentsDto = new ArrayList<DepartmentDto>();
			for (Department department : departments) {
				DepartmentDto departmentDto = conv.fromDepartment(department);
				departmentsDto.add(departmentDto);
			}
			return departmentsDto;
		} catch (EaoExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new SecurityExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityExcepcion(e.getMessage());
		}
	}
	
	/*@Override
	public List<DepartmentDto> findByUser(Integer userId)
			throws CustomException {
		try {
			List<Department> departments = eao.findByUser(userId);
			List<DepartmentDto> departmentsDto = new ArrayList<DepartmentDto>();
			for (Department department : departments) {
				DepartmentDto departmentDto = conv.fromDepartment(department);
				departmentsDto.add(departmentDto);
			}
			return departmentsDto;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException(e.getMessage());
		}
	}*/

}
